#! /bin/bash

# example of usage: ./battle4.sh a1 a10 j1 j10 c1c2 h1h2 e1f1 h8h10 d8d10 f5f8

if [[ $# -ne 10 ]] # - number of ships
then
	echo Wrong number of ships.
	exit
fi

#	initialization
				# for field
for (( i=1; i<101;i++));    
do
arr[$i]='~'
done

i=0
for l in a b c d e f g h i j # for index
do
let "i=$i+1"
ind[$i]=$l
# echo -n $i'='${ind[i]}' '
done

#	processing of parameters


function index()  
{
for i in `seq 1 10`
do
if [[ ${1:0:1} = ${ind[$i]} ]] 
then
case ${#1} in

(2) # b2
let "ret = (${1:1:1}-1)*10+$i"
arr[$ret]=+ 
return ;;

(3) 	# b10
arr[ (${1:1:2}-1)*10+$i ]=+  
return ;;

(4)
if [[ ${1:1:1} -eq ${1:3:1} ]] # string

then

l=$i;
while [[ ${ind[$l]} != ${1:2:1} ]]
do 
arr[ (${1:1:1}-1)*10+$l ]=+   
let "l=$l+1"
done
arr[ (${1:1:1}-1)*10+$l ]=+ # the last +
let "ship = $l-$i" # flag for too big ships
if [[ $ship >=3 ]]
then 
	echo Error of input: the ship $1 is too big.
	exit 0;
	fi


elif [[ ${1:0:1} -eq ${1:2:1} ]] # column
then
for (( l=${1:1:1}; l<=${1:3:1};l++));
do 
arr[ ($l-1)*10+$i ]=+  
done
let "ship = ${1:3:1}-${1:1:1}" # flag for too big ships
if [[ $ship >4 ]]
then 
	echo Error of input: the ship $1 is too big.
	exit 0;
	fi

fi
return ;;

(5)
if [[ ${1:0:1} -eq ${1:2:1} ]] # column b6b10
then
for (( l=${1:1:1}; l<=${1:3:2};l++));
do 
arr[ ($l-1)*10+$i ]=+  
done
let "ship = ${1:3:2}-${1:1:1}" # flag for too big ships
if [[ $ship >3 ]]
then 
	echo Error of input: the ship $1 is too big.
	exit 0;
	fi

fi
return ;;

(6)	# d10e10
l=$i;
while [[ ${ind[$l]} != ${1:3:1} ]]
do 
arr[ (${1:1:2}-1)*10+$l ]=+   
let "l=$l+1"
done
arr[ (${1:1:2}-1)*10+$l ]=+ # the last +
let "ship = $l-$i" # flag for too big ships
if [[ $ship > 3 ]]
then 
	echo Error of input: the ship $1 is too big.
	exit 0;
	fi

return ;;


esac	
fi

 done
}

while [ $# -ne 0 ]
do
#arr[$(index $1 )]=+
index $1 $arr
shift
done
#	output

echo '        ' a b c d e f g h i j

l=1
echo -n "$l"
printf "\t"

for (( i=1; i<101;i++));
do
	echo -n ' '${arr[$i]}
if [ `expr $i % 10` -eq  0 ]; 
then
	let l=$l+1
	printf "\n"
		if [ $l -le 10 ];
		then
			echo -n $l
		printf "\t"
		fi
fi
done

